<div id="<?= $name;?>" class="ctype-field">
	<?php $conf_label = array(); ?>
	<?php if(form_error($name)): ?>
		<?php $conf_label['class'] = "input_error"; ?>
	<?php endif; ?>
			
	<?php $title_label = $properties['title']; ?>
	<?php if(isset($required) && $required): ?>
		<?php $title_label .=  " *"; ?>
	<?php endif; ?>
	<?= form_label($title_label, $name, $conf_label);?>
	
	<div id="<?= $name;?>_results">
	
	<?php $value = set_value($name, $value); ?>
	<?php if($value != NULL && $value != "" && !is_array($value)):?>
		<?php $value = array($value);?>
	<?php elseif($value == NULL || $value == ""):?>
		<?php $value = array();?>
	<?php endif;?>
	
	<?php if($value != NULL):?>
		<?php foreach($value as $file):?>
		<div class="file">
			<span class="title"><?= basename($file); ?></span>
			<a href="#delete" class="delete">Eliminar</a>
			<a href="<?= $file;?>" class="display">Ver</a>
			<input type="hidden" name="<?= $name;?>[]" value="<?= $file; ?>" />
		</div>
		<?php endforeach;?>
	<?php endif;?>
					
	</div>
	
	<input type="file" id="<?= $name;?>_input_file"/>
</div>
<style>
.file .title{
	cursor: move;
}
</style>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
	var <?= $name;?>_count = <?= count($value); ?>
	
	//Ordenables
	$("#<?= $name;?>_results").sortable({ items: 'div.file', axis: 'y' });

	$("#<?= $name;?>_results .file .delete").live("click", function(){
		$(this).parent(".file").fadeTo(400, 0, function() {
			$(this).slideUp(400, function(){
				$(this).remove();

				<?= $name;?>_count--;
				
				<?php if(!isset($properties['multiple']) || $properties['multiple'] == FALSE): ?>
				if(<?= $name;?>_count == 0){
					$("#<?= $name;?>_input_file").removeAttr('disabled');
				}
				<?php endif; ?>
			});
		});

		return false;
	});

	$("#<?= $name;?>_results .file .display").live("click", function(){
		//alert("<?= site_url("");?>" + $(this).attr('href'));
		window.open("<?= base_url();?>" + $(this).attr('href'));
		return false;
	});
	
	$("#<?= $name;?>_input_file").ajaxFileUpload({
		url: "<?= base_url() ."application/libraries/ctype/views/fields/includes/upload.php";?>",
		name: "userfile",
		
		onSubmit: function(file, ext){
			<?php if(isset($properties['upload_allow_filetypes'])): ?>
			if (! (ext && /^(<?= $properties['upload_allow_filetypes']; ?>)$/.test(ext))){
				// check for valid file extension
				alert('<?= $properties['upload_allow_filetypes_msg']; ?>');
				return false;
			}
			<?php endif; ?>

			<?php if(!isset($properties['multiple']) || $properties['multiple'] == FALSE):?>
			$("#<?= $name;?>_input_file").attr('disabled', true);
			<?php endif;?>
		},
		success: function (file, data, status) {
	        if(data.result){
				//Subido correctamente
				$("#<?= $name;?>_results").append('\
					<div class="file">\
						<span class="title">'+file+'</span>\
						<a href="#delete" class="delete">Eliminar</a>\
						<a href="'+data.filerelativepath+'" class="display">Ver</a>\
						<input type="hidden" name="<?= $name;?>[]" value="'+data.filerelativepath+'" />\
					</div>\
				');

				<?= $name;?>_count++;
						
	        } else{
		        if(data.message){
					alert(data.message);
			    } else{
					alert("A ocurrido un error subiendo el archivo");
			    }
				$("#<?= $name;?>_input_file").removeAttr('disabled');
	        }
        },
        error: function (data, status, e){
            alert(e);
            $("#<?= $name;?>_input_file").removeAttr('disabled');
        }
	});

	<?php if((!isset($properties['multiple']) || $properties['multiple'] == FALSE) && count($value) > 0):?>
		$("#<?= $name;?>_input_file").attr('disabled', true);
	<?php endif;?>

});
</script>